home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_se / id_provider.e < prev    next >
Text File  |  2000-03-25  |  6KB  |  216 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class ID_PROVIDER
  17.    --
  18.    -- Unique object in charge of some id providing.
  19.    --
  20.  
  21. inherit GLOBALS;
  22.  
  23. creation make
  24.  
  25. feature {SMALL_EIFFEL}
  26.  
  27.    max_id: INTEGER;
  28.  
  29. feature {COMPILE_TO_C}
  30.  
  31.    disk_save is
  32.       local
  33.          i, id: INTEGER;
  34.          sfw: STD_FILE_WRITE;
  35.          str: STRING;
  36.       do
  37.          !!sfw.make;
  38.          echo.sfw_connect(sfw,system_tools.id_file_path);
  39.          from
  40.             i := 1;
  41.          until
  42.             i > id_memory.count
  43.          loop
  44.             str := id_memory.key(i);
  45.             id := id_memory.item(i);
  46.             sfw.put_integer(id);
  47.             sfw.put_character(' ');
  48.             sfw.put_character('%"');
  49.             sfw.put_string(str);
  50.             sfw.put_character('%"');
  51.             sfw.put_character(' ');
  52.             small_eiffel.id_extra_information(sfw,str);
  53.             sfw.put_character('%N');
  54.             i := i + 1;
  55.          end;
  56.          sfw.disconnect;
  57.       end;
  58.  
  59. feature {EIFFEL_PARSER,BASE_CLASS,RUN_CLASS}
  60.  
  61.    item(str: STRING): INTEGER is
  62.       require
  63.          str = string_aliaser.item(str)
  64.       do
  65.          if id_memory.has(str) then
  66.             Result := id_memory.at(str);
  67.          else
  68.             max_id := max_id + 1;
  69.             Result := max_id;
  70.             id_memory.put(Result,str);
  71.          end;
  72.       end;
  73.  
  74. feature {POSITION}
  75.  
  76.    alias_of(id: INTEGER): STRING is
  77.       do
  78.          Result := id_memory.fast_key_at(id);
  79.       end;
  80.  
  81. feature {NONE}
  82.  
  83.    id_memory: DICTIONARY[INTEGER,STRING] is
  84.       once
  85.          !!Result.with_capacity(2048);
  86.       end;
  87.  
  88.    make is
  89.       do
  90.          id_memory.put(1,as_general);
  91.          id_memory.put(2,as_integer);
  92.          id_memory.put(3,as_character);
  93.          id_memory.put(4,as_real);
  94.          id_memory.put(5,as_double);
  95.          id_memory.put(6,as_boolean);
  96.          id_memory.put(7,as_string);
  97.          id_memory.put(8,as_pointer);
  98.          id_memory.put(9,as_native_array_character);
  99.          id_memory.put(10,as_any);
  100.          max_id := 10;
  101.          disk_restore;
  102.       end;
  103.  
  104.    disk_restore is
  105.       local
  106.      cc: CHARACTER;
  107.          type_name: STRING;
  108.          id, item_count: INTEGER;
  109.          sfr: STD_FILE_READ;
  110.      state: INTEGER;
  111.      -- state = 0 : waiting first digit of `id'.
  112.      -- state = 1 : inside `id'.
  113.      -- state = 2 : waiting opening ".
  114.      -- state = 3 : inside `type_name'.
  115.      -- state = 4 : waiting end of line.
  116.      -- state = 5 : final success.
  117.          -- state = 6 : final error.
  118.       do
  119.          !!sfr.make;
  120.          echo.sfr_connect(sfr,system_tools.id_file_path);
  121.          if sfr.is_connected then
  122.             from
  123.            if sfr.end_of_input then
  124.           state := 6;
  125.            end;
  126.             until
  127.            state > 4
  128.             loop
  129.            sfr.read_character;
  130.            if sfr.end_of_input then
  131.           state := 5;
  132.            else
  133.           cc := sfr.last_character;
  134.            end;
  135.            inspect
  136.           state
  137.            when 0 then
  138.           inspect
  139.              cc
  140.           when ' ', '%R', '%N', '%T' then
  141.           when '0' .. '9' then
  142.              id := cc.decimal_value;
  143.              state := 1;
  144.           else
  145.              state := 6;
  146.           end;
  147.            when 1 then
  148.           inspect
  149.              cc
  150.           when '0' ..'9' then
  151.              id := id * 10 + cc.decimal_value;
  152.           when '%"' then
  153.              type_name := temporary_type_name;
  154.              type_name.clear;
  155.              state := 3;
  156.           when ' ', '%T' then
  157.              state := 2;
  158.           else
  159.              state := 6;
  160.           end;
  161.            when 2 then
  162.           inspect
  163.              cc
  164.           when '%"' then
  165.              type_name := temporary_type_name;
  166.              type_name.clear;
  167.              state := 3;
  168.           when ' ', '%T', '%N', '%R' then
  169.           else
  170.              state := 6;
  171.           end;
  172.            when 3 then
  173.           inspect
  174.              cc
  175.           when '%"' then
  176.              type_name := string_aliaser.item(type_name);
  177.              item_count := item_count + 1;
  178.              id_memory.put(id,type_name);
  179.              max_id := max_id.max(id);
  180.              state := 4;
  181.           when '%N', '%R', '%T' then
  182.              state := 6;
  183.           else
  184.              type_name.extend(cc);
  185.           end;
  186.            when 4 then
  187.           inspect
  188.              cc
  189.           when '%N', '%R' then
  190.              state := 0;
  191.           else
  192.           end;
  193.            else
  194.            end;
  195.             end;
  196.             sfr.disconnect;
  197.         if state = 6 then
  198.            echo.put_string("Corrupted *.id file (after ");
  199.            echo.put_integer(item_count);
  200.            echo.put_string(" correct items).%N");
  201.         end;
  202.             echo.put_string("Previous IDs reloaded (");
  203.             echo.put_integer(id_memory.count);
  204.             echo.put_character('/');
  205.             echo.put_integer(max_id);
  206.             echo.put_string(").%N");
  207.          end;
  208.       end;
  209.  
  210.    temporary_type_name: STRING is
  211.       once
  212.      !!Result.make(128);
  213.       end;
  214.  
  215. end -- ID_PROVIDER
  216.